# coding=utf-8

import sys
import os.path

FUNC_TEMPL = """
int func{N}()
{LP}
    return {N};
{RP}
"""

FUNC_DECL_TEMPL = """int func{N}();
"""

FUNC_CALL_TEMPL = """    cout << func{N}() << endl;
"""

MAIN_TEMPL = """#include <iostream>
using namespace std;
{FUNC_DECLS}
int main()
{LP}
{FUNC_CALLS}
    return 0;
{RP}
"""

def MainCode(n):
    FUNC_DECLS = ''.join([FUNC_DECL_TEMPL.format(N=i) for i in range(n)])
    FUNC_CALLS = ''.join([FUNC_CALL_TEMPL.format(N=i) for i in range(n)])
    return MAIN_TEMPL.format(FUNC_DECLS=FUNC_DECLS, FUNC_CALLS=FUNC_CALLS, LP="{", RP="}")

def FuncCode(n):
    return FUNC_TEMPL.format(N=n, LP="{", RP="}")

FUNC_FILE_NAME_TEMPL = "func{N}.cpp"
MAIN_FILE_NAME= "main.cpp"

def CreateSourceFiles(path, n):
    for i in range(n):
        FUNC_FILE_NAME = os.path.join(path, FUNC_FILE_NAME_TEMPL.format(N=i))
        with open(FUNC_FILE_NAME, "w") as f:
            f.write(FuncCode(i))

    with open(MAIN_FILE_NAME, "w") as f:
        f.write(MainCode(n))

def GetSourceFiles(n):
    for i in range(n):
        FUNC_FILE_NAME = FUNC_FILE_NAME_TEMPL.format(N=i)
        print(FUNC_FILE_NAME)
    print(MAIN_FILE_NAME)

OPTION = ""
OUTPUT_PATH = ""
def CheckCmdLine():
    ARG_LEN = len(sys.argv)
    if ARG_LEN not in (2, 3):
        print("Usage: %s <option> [<path>]" % sys.argv[0])
        return False

    global OPTION
    OPTION = sys.argv[1]
    if OPTION not in ("CreateFiles", "GetFiles"):
        print("<option> must be 'CreateFiles' or `GetFiles'")
        return False

    if OPTION == "CreateFiles":
        if ARG_LEN == 2:
            print("<path> is missing in 'CreateFiles' mode")
            return False
        global OUTPUT_PATH
        OUTPUT_PATH = sys.argv[2]
    return True

FILE_COUNT = 5
if __name__ == "__main__":
    if not CheckCmdLine():
        exit(-1)

    if OPTION == "CreateFiles":
        CreateSourceFiles(OUTPUT_PATH, FILE_COUNT)
    elif OPTION == "GetFiles":
        GetSourceFiles(FILE_COUNT)
